<%--
  Created by IntelliJ IDEA.
  User: 秋梓
  Date: 2021/12/6
  Time: 11:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="queryServletBackStage" method="get">
    --------------- 图书管理 ---------------
    <select name="type" id="type">
        <c:forEach items="${types}" var="type">
            <c:if test="${sessionScope.typeSelected == type}">
                <option value="${type}" selected>
                    <c:out value="${type}"/>
                </option>
            </c:if>
            <c:if test="${sessionScope.typeSelected != type}">
                <option value="${type}">
                    <c:out value="${type}"/>
                </option>
            </c:if>
        </c:forEach>
    </select>
    <input name="keyword" placeholder="输入书名" value="${sessionScope.queryStr}">
    <button>搜索</button>
</form>

<table table-layout="fixed">
    <c:forEach items="${books}" var="book">
        <c:if test="${sessionScope.map.get(book.getId()) == -1}">
            <tr>
                <td><button onclick="deleteBookFromBooks(${book.getId()})">下架</button></td>
                <td><c:out value="序列号:${book.getId()}"/></td>
                <td><c:out value="书名:《${book.getName()}》"/></td>
                <td><c:out value="作者:${book.getAuthor()}"/></td>
                <td><c:out value="类型:${book.getType()}"/></td>
                <td>&nbsp;<c:out value="价格:${book.getPrice()}"/></td>
                <td>￥</td>
                <td>&nbsp;<c:out value="库存:${book.getInventory()}"/></td>
                <td style="max-width: 120px; overflow: hidden;text-overflow: ellipsis" title="${book.getImgPath()}">
                    <a onclick="window.open('${book.getImgPath()}')">${book.getImgPath()}</a>
                </td>
                <td><button onclick="editStatusChange(${book.getId()}, 1, '${book.getDetails()}')">修改</button></td>
                <td><a onclick="detailsShowBackStage(${book.getId()})">详情</a></td>
                <c:if test="${sessionScope.detailsShowMap.get(book.getOne()) == book.getId()}">
                    <td rowspan="4">
                        <div class="details-show">
                            <c:out value="${book.getDetails()}"/>
                        </div>
                    </td>
                </c:if>
            </tr>
        </c:if>
        <c:if test="${sessionScope.map.get(book.getId()) == 1}">
            <form action="editBookFromBooksServlet" method="post">
                <tr>
                    <td><button onclick="deleteBookFromBooks(${book.getId()})">下架</button></td>
                    <td><c:out value="序列号:${book.getId()}"/>
                        <input name="bookid" value="${book.getId()}" style="display: none">
                    </td>
                    <td><c:out value="书名:"/><input name="bookname" value="${book.getName()}"/></td>
                    <td>作者:<input name="bookauthor" value="${book.getAuthor()}"/></td>
                    <td><c:out value="类型:"/>
                        <select name="booktype">
                            <c:forEach items="${typesSetting}" var="type">
                                <c:if test="${sessionScope.typeSelected == type}">
                                    <option value="${type}" selected>
                                        <c:out value="${type}"/>
                                    </option>
                                </c:if>
                                <c:if test="${sessionScope.typeSelected != type}">
                                    <option value="${type}">
                                        <c:out value="${type}"/>
                                    </option>
                                </c:if>
                            </c:forEach>
                        </select>
                    </td>
                    <td>&nbsp;<c:out value="价格:"/><input name="bookprice" value="${book.getPrice()}"/></td>
                    <td>￥</td>
                    <td>&nbsp;<c:out value="库存:"/><input name="bookinventory" value="${book.getInventory()}"/></td>
                        <%--                    <td>图:<input type="file" name="bookimg" value="${book.getImgPath()}"></td>--%>
                    <td><input name="bookdetails" id="insizeDetails" style="display: none"/></td>
                    <button id="insizeBtn" style="display: none"></button>
            </form>
            <td><button onclick="editStatusChange(${book.getId()}, -1, '${book.getDetails()}')">保存</button></td>
            <td><a onclick="detailsShowBackStage(${book.getId()})">详情</a></td>
            <c:if test="${sessionScope.detailsShowMap.get(book.getOne()) == book.getId()}">
                <td rowspan="4">
                        <%--                    <input class="details-edit" type="text" value="${book.getDetails()}"/>--%>
                    <textarea id="outsizeDetails" class="details-edit" cols="4"><c:out value="${book.getDetails()}"/></textarea>
                </td>
            </c:if>
            </tr>
        </c:if>
    </c:forEach>

    <c:if test="${books.toString() == '[]'}">
        （竟然什么也搜不到=。=）
    </c:if>

    <tr><td colspan="9">--------------------------------------------------------------------------------
        ----------------------------------------------------------------------</td></tr>
    <form action="addBookIntoBooksServlet" method="post">
        <tr>
            <td><c:out value="新增书目:"/></td>
            <td><c:out value="序列号:*"/>&nbsp;</td>
            <td><c:out value="书名:"/><input name="bookname" id="bookname" placeholder="输入书名"/></td>
            <td> 作者:<input name="bookauthor" id="bookauthor"/></td>
            <td><c:out value="类型:"/><select name="booktype" id="booktype">
                <c:forEach items="${typesSetting}" var="type">
                    <c:if test="${sessionScope.typeSelected == type}">
                        <option value="${type}" selected>
                            <c:out value="${type}"/>
                        </option>
                    </c:if>
                    <c:if test="${sessionScope.typeSelected != type}">
                        <option value="${type}">
                            <c:out value="${type}"/>
                        </option>
                    </c:if>
                </c:forEach>
            </select>
            </td>
            <td>&nbsp;<c:out value="价格:"/><input name="bookprice" id="bookprice" placeholder="输入单价"/></td>
            <td>￥
                <c:if test="${sessionScope.allow != null}">
                    <button id="insizeBtn-1" style="display: none"></button>
                </c:if>
            </td>
            <td>&nbsp;<c:out value="库存:"/><input name="bookinventory" id="bookinventory" placeholder="输入库存"/></td>
    </form>
    <td><button onclick="twoPartFormSubmit()">上架</button></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <form action="imgUploadServlet" id="imgUpload" method="post" enctype="multipart/form-data">
            <td>图片上传:</td>
            <td>
                <input name="bookimg" type="file">
                <button id="insizeBtn-2" style="display: none"></button>
            </td>
        </form>
        <form action="formDetailsPartServlet" method="post">
            <td colspan="5">
                详情:<input class="details-new" name="bookdetails" id="bookdetails" placeholder="输入详情">
                <c:if test="${sessionScope.allow0 != null}">

                    <button id="form-details-btn" style="display: none"></button>
                </c:if>
            </td>
        </form>
    </tr>
    <tr><td>&nbsp;</td></tr>
    <tr><td>&nbsp;</td></tr>
</table>
</form>
</body>
</html>

<script>
    function deleteBookFromBooks (id) {
        window.location.href = "http://localhost:8080/JavaBookShop/deleteBookFromBooksServlet?id="+id;
    }

    function editStatusChange (id, status, before) {
        detailsShowBackStage(-999);
        window.location.href = "http://localhost:8080/JavaBookShop/bookEditStatusChangeServlet?id="+id+"&status="+status;
        if (status == -1) {
            var details = document.getElementById("outsizeDetails");
            if (details != null) {
                document.getElementById("insizeDetails").value=details.value;
            } else {
                document.getElementById("insizeDetails").value=before;
            }
            document.getElementById("insizeBtn").click();
        }
    }

    function detailsShowBackStage (id) {
        window.location.href = "http://localhost:8080/JavaBookShop/detailsShowBackStageServlet?id="+id;
    }

    function twoPartFormSubmit () {
        sessionStorage.setItem("bookname", document.getElementById("bookname").value);
        sessionStorage.setItem("bookauthor", document.getElementById("bookauthor").value);
        sessionStorage.setItem("booktype", document.getElementById("booktype").value);
        sessionStorage.setItem("bookprice", document.getElementById("bookprice").value);
        sessionStorage.setItem("bookinventory", document.getElementById("bookinventory").value);
        sessionStorage.setItem("bookdetails", document.getElementById("bookdetails").value);
        document.getElementById("insizeBtn-2").click();
    }

</script>
